home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2010 Summer - Disc 1 / WN_Ete2010_CD1.iso / Onglet5 / Weezo / Weezo setup.exe / {code_appDir} / www / includes / applicationConfigFunctions.php < prev    next >
PHP Script  |  2010-05-19  |  37KB  |  773 lines

  1. <?php
  2.  
  3. /**
  4.  * @desc transform an IP mask with "*" by an httpd formated IP mask
  5.  *
  6.  * @param strin $address : ip adress matching cfIsIP rule
  7.  * @return string : mask
  8.  */
  9. function setHttpdIpMask($address){
  10.     if(strpos($address,'*')===false) return $address;
  11.     $mask=str_replace('*','1',$address).'/';
  12.     $bytes=explode('.',$address);
  13.     foreach ($bytes as $byte) {
  14.         if($byte=='*') $mask.='.0'; else $mask.='.255';
  15.     }
  16.     return str_replace('/.','/',$mask);
  17. }
  18.  
  19. /**
  20.  * @desc write document root .htaccess with general included or excluded adresses
  21.  *
  22.  */
  23. function writeHtaccess(){
  24.     if(cfGGetVar('IPFilter')=='no') return @unlink(cfAppDocRoot().'/.htaccess');
  25.     // Write file
  26.     if($fp=fopen(cfAppDocRoot().'/.htaccess','w')){
  27.         fwrite($fp,"<LIMIT GET POST>\n");
  28.         if(cfGGetVar('IPFilter')=='include'){
  29.             $IPFilterIncluded=explode(';',cfGGetVar('IPFilterIncluded'));
  30.             if(count($IPFilterIncluded)){
  31.                 fwrite($fp,"Order deny,allow\nDeny from all\n");
  32.                 foreach ($IPFilterIncluded as $key => $value) if($value!='') fwrite($fp,'Allow from '.setHttpdIpMask($value)."\n");
  33.             }
  34.         }
  35.         elseif(cfGGetVar('IPFilter')=='exclude'){
  36.             $IPFilterExcluded=explode(';',cfGGetVar('IPFilterExcluded'));
  37.             if(count($IPFilterExcluded)){
  38.                 fwrite($fp,"Order allow,deny\nAllow from all\n");
  39.                 foreach ($IPFilterExcluded as $key => $value) if($value!='') fwrite($fp,'Deny from '.setHttpdIpMask($value)."\n");
  40.             }
  41.         }
  42.         fwrite($fp,"</LIMIT>");
  43.         fclose($fp);
  44.     }
  45. }
  46.  
  47. /**
  48.  * @desc display and process IP restriction Form
  49.  *
  50.  * @param string $environment : 'application' if launched from UI, 'browser' if launched from web browser
  51.  * @param mixed $userFile : - false for general IP restrictions
  52.  *                             - user configuration file name for user IP restrictions
  53.  *                             - or user ID for for user IP restrictions
  54.  */
  55. function acIPForm($environment, $userFile=false){
  56.     function checkLocalIsSet(){
  57.         if(strpos(cfGGetVar('IPFilterIncluded'),'127.0.0.1')===false){
  58.             if(!cfGGetVar('IPFilterIncluded')) cfGSetVar('IPFilterIncluded','127.0.0.1'); else cfGSetVar('IPFilterIncluded','127.0.0.1;'.cfGGetVar('IPFilterIncluded'));
  59.         }
  60.     }
  61.     $_ENV['configurationEnvironment']=$environment;
  62.  
  63. /*
  64.  ***************************************************************************************************************************
  65.  * Process POST commands
  66.  ***************************************************************************************************************************
  67.  */
  68.     if(isset($_POST['action']) && isset($_POST['userFile']) && ($_POST['userFile']=='general' || file_exists(cfAppDataDir().'/'.cfUTF8Decode($_POST['userFile'])))){
  69.         $userFileName=cfAppDataDir().'/'.cfUTF8Decode($_POST['userFile']);
  70.  
  71.         // Read user data if needed
  72.         if($_POST['userFile']!='general') {
  73.             require_once(INCLUDE_DIR.'resourceConfigFunctions.php');
  74.             $userData=cfParse_ini_file($userFileName);
  75.             if(!isset($userData['IPFilterIncluded'])) $userData['IPFilterIncluded']='';
  76.             if(!isset($userData['IPFilterExcluded'])) $userData['IPFilterExcluded']='';
  77.         }
  78.  
  79.         // Change filter
  80.         if($_POST['action']=='changeFilter' && ($_POST['mode']=='no' || $_POST['mode']=='include' || $_POST['mode']=='exclude' || $_POST['mode']=='LAN')){
  81.             if($_POST['userFile']=='general'){
  82.                 if($_POST['mode']=='LAN'){
  83.                     cfGSetVar('IPFilter','include');
  84.                     cfGSetVar('IPFilterIncluded','127.0.0.1;10.*.*.*;192.168.*.*');
  85.                     cfServerSendCommand('changeParameter name="IPFilterIncluded" value="'.cfGGetVar('IPFilterIncluded').'"');
  86.                 }
  87.                 else cfGSetVar('IPFilter',$_POST['mode']);
  88.                 cfServerSendCommand('changeParameter name="IPFilter" value="'.cfGGetVar('IPFilter').'"');
  89.                 // Commit to .htaccess
  90.                 writeHtaccess();
  91.             }
  92.             else{
  93.                 if($_POST['mode']=='LAN'){
  94.                     $userData['IPFilter']='include';
  95.                     $userData['IPFilterIncluded']='127.0.0.1;10.*.*.*;192.168.*.*';
  96.                 }
  97.                 else $userData['IPFilter']=$_POST['mode'];
  98.                 rcWriteUserFile($userData,cfUTF8Decode($_POST['userFile']));
  99.             }
  100.         }
  101.         // Add an included IP
  102.         elseif($_POST['action']=='addInclude' && isset($_POST['newInclude']) && cfIsIP($_POST['newInclude'])){
  103.             // General setting
  104.             if($_POST['userFile']=='general'){
  105.                 if(strpos(cfGGetVar('IPFilterIncluded'),$_POST['newInclude'])===false){
  106.                     if(!cfGGetVar('IPFilterIncluded') || strlen(cfGGetVar('IPFilterIncluded'))==0)
  107.                         cfGSetVar('IPFilterIncluded',$_POST['newInclude']);
  108.                     else
  109.                         cfGSetVar('IPFilterIncluded',cfGGetVar('IPFilterIncluded').';'.$_POST['newInclude']);
  110.                     checkLocalIsSet();
  111.                     // Save to server
  112.                     cfServerSendCommand('changeParameter name="IPFilterIncluded" value="'.cfGGetVar('IPFilterIncluded').'"');
  113.                     cfGSetVar('IPFilter','include');
  114.                     cfServerSendCommand('changeParameter name="IPFilter" value="include"');
  115.                     // Commit to .htaccess
  116.                     writeHtaccess();
  117.                 }
  118.             }
  119.             else{
  120.                 // Per user setting
  121.                 $userData['IPFilter']='include';
  122.                 if(strpos($userData['IPFilterIncluded'],$_POST['newInclude'])===false){
  123.                     if(!isset($userData['IPFilterIncluded']) || strlen($userData['IPFilterIncluded'])==0)
  124.                         $userData['IPFilterIncluded']=$_POST['newInclude'];
  125.                     else
  126.                         $userData['IPFilterIncluded'].=';'.$_POST['newInclude'];
  127.                     // Save modifications
  128.                     rcWriteUserFile($userData,cfUTF8Decode($_POST['userFile']));
  129.                 }
  130.             }
  131.         }
  132.         // Add an excluded IP
  133.         elseif($_POST['action']=='addExclude' && isset($_POST['newExclude']) && cfIsIP($_POST['newExclude'])){
  134.             // General setting
  135.             if($_POST['userFile']=='general'){
  136.                 if(strpos(cfGGetVar('IPFilterExcluded'),$_POST['newExclude'])===false){
  137.                     if(!cfGGetVar('IPFilterExcluded') || strlen(cfGGetVar('IPFilterExcluded'))==0)
  138.                         cfGSetVar('IPFilterExcluded',$_POST['newExclude']);
  139.                     else
  140.                         cfGSetVar('IPFilterExcluded',cfGGetVar('IPFilterExcluded').';'.$_POST['newExclude']);
  141.                     // Save to server
  142.                     cfServerSendCommand('changeParameter name="IPFilterExcluded" value="'.cfGGetVar('IPFilterExcluded').'"');
  143.                     cfGSetVar('IPFilter','exclude');
  144.                     cfServerSendCommand('changeParameter name="IPFilter" value="exclude"');
  145.                     // Commit to .htaccess
  146.                     writeHtaccess();
  147.                 }
  148.             }
  149.             else{
  150.                 // Per user setting
  151.                 $userData['IPFilter']='exclude';
  152.                 if(strpos($userData['IPFilterExcluded'],$_POST['newExclude'])===false){
  153.                     if(!isset($userData['IPFilterExcluded']) || strlen($userData['IPFilterExcluded'])==0)
  154.                         $userData['IPFilterExcluded']=$_POST['newExclude'];
  155.                     else
  156.                         $userData['IPFilterExcluded'].=';'.$_POST['newExclude'];
  157.                     // Save modifications
  158.                     rcWriteUserFile($userData,cfUTF8Decode($_POST['userFile']));
  159.                 }
  160.             }
  161.         }
  162.         // Remove an included IP
  163.         elseif($_POST['action']=='removeIncludedIP' && isset($_POST['removedIP'])){
  164.             if($_POST['userFile']=='general'){
  165.                 $IPFilterIncluded=explode(';',cfGGetVar('IPFilterIncluded'));
  166.                 if(isset($IPFilterIncluded[$_POST['removedIP']])){
  167.                     unset($IPFilterIncluded[$_POST['removedIP']]);
  168.                     cfGSetVar('IPFilterIncluded',implode(';',$IPFilterIncluded));
  169.                     checkLocalIsSet();
  170.                     // Save to server
  171.                     cfServerSendCommand('changeParameter name="IPFilterIncluded" value="'.cfGGetVar('IPFilterIncluded').'"');
  172.                     if(count($IPFilterIncluded)) cfGSetVar('IPFilter','include'); else cfGSetVar('IPFilter','no');
  173.                     cfServerSendCommand('changeParameter name="IPFilter" value="include"');
  174.                     // Commit to .htaccess
  175.                     writeHtaccess();
  176.                 }
  177.             }
  178.             else{
  179.                 $IPFilterIncluded=explode(';',$userData['IPFilterIncluded']);
  180.                 if(isset($IPFilterIncluded[$_POST['removedIP']])){
  181.                     unset($IPFilterIncluded[$_POST['removedIP']]);
  182.                     $userData['IPFilterIncluded'] = implode(';',$IPFilterIncluded);
  183.                     if(count($IPFilterIncluded)) $userData['IPFilter']='include'; else $userData['IPFilter']='no';
  184.                     // Save modifications
  185.                     rcWriteUserFile($userData,cfUTF8Decode($_POST['userFile']));
  186.                 }
  187.             }
  188.         }
  189.         // Remove an excluded IP
  190.         elseif($_POST['action']=='removeExcludedIP' && isset($_POST['removedIP'])){
  191.             if($_POST['userFile']=='general'){
  192.                 $IPFilterExcluded=explode(';',cfGGetVar('IPFilterExcluded'));
  193.                 if(isset($IPFilterExcluded[$_POST['removedIP']])){
  194.                     unset($IPFilterExcluded[$_POST['removedIP']]);
  195.                     cfGSetVar('IPFilterExcluded',implode(';',$IPFilterExcluded));
  196.                     // Save to server
  197.                     cfServerSendCommand('changeParameter name="IPFilterExcluded" value="'.cfGGetVar('IPFilterExcluded').'"');
  198.                     if(count($IPFilterExcluded)) cfGSetVar('IPFilter','exclude'); else cfGSetVar('IPFilter','no');
  199.                     cfServerSendCommand('changeParameter name="IPFilter" value="exclude"');
  200.                     // Commit to .htaccess
  201.                     writeHtaccess();
  202.                 }
  203.             }
  204.             else{
  205.                 $IPFilterExcluded=explode(';',$userData['IPFilterExcluded']);
  206.                 if(isset($IPFilterExcluded[$_POST['removedIP']])){
  207.                     unset($IPFilterExcluded[$_POST['removedIP']]);
  208.                     $userData['IPFilterExcluded'] = implode(';',$IPFilterExcluded);
  209.                     if(count($IPFilterExcluded)) $userData['IPFilter']='exclude'; else $userData['IPFilter']='no';
  210.                     // Save modifications
  211.                     rcWriteUserFile($userData,cfUTF8Decode($_POST['userFile']));
  212.                 }
  213.             }
  214.         }
  215.     }
  216.  
  217.  
  218.     // Load parameters
  219.     if($userFile===false || $userFile=='general'){
  220.         $userFile='general';
  221.         if(cfGGetVar('IPFilter')) $IPFilter=cfGGetVar('IPFilter'); else $IPFilter='no';
  222.         $IPFilterIncluded=explode(';',cfGGetVar('IPFilterIncluded'));
  223.         $IPFilterExcluded=explode(';',cfGGetVar('IPFilterExcluded'));
  224.     }
  225.     elseif(file_exists(cfAppDataDir().'/'.$userFile) && cfFileExtension($userFile)=='usr'){
  226.         $userData=cfParse_ini_file(cfAppDataDir().'/'.$userFile);
  227.         if(isset($userData['IPFilter'])) $IPFilter=$userData['IPFilter']; else $IPFilter='no';
  228.         if(!isset($userData['IPFilterIncluded'])) $userData['IPFilterIncluded']='';
  229.         if(!isset($userData['IPFilterExcluded'])) $userData['IPFilterExcluded']='';
  230.         $IPFilterIncluded=explode(';',$userData['IPFilterIncluded']);
  231.         $IPFilterExcluded=explode(';',$userData['IPFilterExcluded']);
  232.     }
  233.     else outDisplayErrorPage(cfCaption('configInvalidFile'));
  234.  
  235. /*
  236.  ***************************************************************************************************************************
  237.  * Insert HEAD, JS functions and form
  238.  ***************************************************************************************************************************
  239.  */
  240.     cfInsertHEAD(false);
  241. ?>
  242. <meta width="480x400"></meta>
  243. <meta icon="warning"></meta>
  244. <meta wintitle="<?php echo cfCaption('configIPFilter');?>"></meta>
  245. </head>
  246. <body width="480" height="360" onContextMenu="return false;" style="cursor:default;overflow:hidden" onSelect="D.deselForm.desel.select()" onload="init()">
  247. <form style="position:absolute;top:-100px" name="deselForm"><input type="text" name="desel" value="a">a</input></form>
  248. <script type="text/javascript" language="javascript">
  249. var filter="<?php echo $IPFilter;?>";
  250. function onrightclick(e){return false}
  251. function init(){wl.UICommand('fadeSize','480x'+(20+actualOffsetBottom(dgi('topDiv'))))}
  252. function cfIsIP(text){
  253.     text=text.replace(/\*/g,"255");
  254.     var IPExp=/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/g;
  255.     if(text.match(IPExp)!=null && text.match(IPExp).length==1) return true; else return false;
  256. }
  257. function changeFilter(newFilter){
  258.     filter=newFilter;
  259.     if(filter=='include') dgi('includeInnerDiv').style.display='inline'; else dgi('includeInnerDiv').style.display='none';
  260.     if(filter=='exclude') dgi('excludeInnerDiv').style.display='inline'; else dgi('excludeInnerDiv').style.display='none';
  261.     dgi('saveButton').style.display="inline";
  262.     dgi('saveButtonD').style.display="none";
  263.     wl.UICommand('fadeSize','480x'+(20+actualOffsetBottom(dgi('topDiv'))));
  264. }
  265. function addInclude(e){
  266.     var kc;
  267.     if(e) {if(e.which!=13) return;}
  268.     else {
  269.         if (window.event && window.event.keyCode){
  270.             if(window.event.keyCode!=13) return;
  271.         }
  272.     }
  273.  
  274.     var IP=dgi('newInclude').value;
  275.     if(!cfIsIP(IP)) alert("<?php echo cfCaptionJS('IPFilterIncorrectIP')?>");
  276.     else{
  277.         D.IPForm.action.value="addInclude";
  278.         D.IPForm.submit();
  279.     }
  280. }
  281. function addExclude(e){
  282.     var kc;
  283.     if(e) {if(e.which!=13) return;}
  284.     else {
  285.         if (window.event && window.event.keyCode){
  286.             if(window.event.keyCode!=13) return;
  287.         }
  288.     }
  289.  
  290.     var IP=dgi('newExclude').value;
  291.     if(!cfIsIP(IP)) alert("<?php echo cfCaptionJS('IPFilterIncorrectIP')?>");
  292.     else{
  293.         D.IPForm.action.value="addExclude";
  294.         D.IPForm.submit();
  295.     }
  296. }
  297.  
  298. function removeIncludedIP(IPid){
  299.     D.IPForm.removedIP.value=IPid;
  300.     D.IPForm.action.value="removeIncludedIP";
  301.     D.IPForm.submit();
  302. }
  303. function removeExcludedIP(IPid){
  304.     D.IPForm.removedIP.value=IPid;
  305.     D.IPForm.action.value="removeExcludedIP";
  306.     D.IPForm.submit();
  307. }
  308. function save(){
  309.     D.IPForm.action.value="changeFilter";
  310.     D.IPForm.submit();
  311. }
  312. function cancel(){}
  313. </script>
  314. <div id="topDiv">
  315.  
  316. <?php
  317. // Check if rules match with LAN restriction
  318. if($IPFilter=='include' && (($userFile && $userFile!='general' && $userData['IPFilterIncluded']=='127.0.0.1;10.*.*.*;192.168.*.*') || ((!$userFile || $userFile=='general') && cfGGetVar('IPFilterIncluded')=='127.0.0.1;10.*.*.*;192.168.*.*'))) $isLAN=true; else $isLAN=false;
  319. /*
  320.  ***************************************************************************************************************************
  321.  * Display IP filter form
  322.  ***************************************************************************************************************************
  323.  */
  324.  
  325. echo outDivFrame('frame1');
  326. echo '<div class="frame1Header">'.cfCaption('IPFilterTitle').'</div><br>';
  327. echo '<form name="IPForm" action="'.$_SERVER['PHP_SELF'].'" method="POST" enctype="multipart/form-data" onsubmit="return false;">';
  328.  
  329. // resource ID
  330. if(isset($_SESSION['activeResourceId'])) echo '<input type="text" name="resId" value="'.$_SESSION['activeResourceId'].'" style="display:none">';
  331.  
  332. // Range
  333. echo '<input type="text" name="userFile" value="'.$userFile.'" style="display:none">';
  334.  
  335. // removedIP
  336. echo '<input type="text" name="removedIP" value="" style="display:none">';
  337.  
  338. // Action
  339. echo '<input type="text" name="action" value="" style="display:none">';
  340.  
  341. echo '<b>';
  342. if($userFile=='general') echo cfCaption('IPFilterGlobal'); else echo cfCaption('IPFilterAccount', cfUTF8Encode($userData['name']));
  343. echo '</b><br/><br/>'.cfCaption('IPFilterNotice')."<br/><br/>\n";
  344.  
  345. // No filtering
  346. echo outDivFrame('frame2');
  347. echo outFrameHeaderTable('frame2Header','<input id="noFilterInput" onclick="javascript:changeFilter(\'no\')" type="radio" name="mode" value="no"'.(($IPFilter=='no')?' checked> ':'> ').'<span style="cursor:pointer" onclick="n=this.previousSibling.previousSibling;n.checked=true;n.onclick()">'.cfCaption('IPFilterNo')).'</span>';
  348. echo '</div>';
  349.  
  350. // Included IP
  351. echo outDivFrame('frame2');
  352. echo outFrameHeaderTable('frame2Header','<input onclick="javascript:changeFilter(\'include\')" type="radio" name="mode" value="include"'.(($IPFilter=='include' && !$isLAN)?' checked> ':'> ').'<span style="cursor:pointer" onclick="n=this.previousSibling.previousSibling;n.checked=true;n.onclick()">'.cfCaption('IPFilterInclude')).'</span>';
  353. echo '<div id="includeInnerDiv" style="display:'.(($IPFilter=='include' && !$isLAN)?'inline':'none')."\">\n";
  354. foreach ($IPFilterIncluded as $key => $value) if($value!=''){
  355.     echo '<span style="margin:1em"></span>'.outButtonSmall('','javascript:removeIncludedIP(\''.$key.'\')',outIcon('cancel'), cfCaption('genDelete')).'<span style="margin-left:2em;vertical-align:top">'.$value.'</span>'."<br/>";
  356. }
  357. echo cfCaption('IPFilterAdd');
  358. echo '<input style="margin: 0 2em 0 2em" class="textInput" type="text" id="newInclude" name="newInclude" size="12" maxlength="15">';
  359. echo outButton('','javascript:addInclude()',outIcon('add'),false,false,'style="vertical-align:bottom"');
  360. echo '</div></div>';
  361.  
  362. // Excluded IP
  363. echo outDivFrame('frame2');
  364. echo outFrameHeaderTable('frame2Header','<input onclick="javascript:changeFilter(\'exclude\')" type="radio" name="mode" value="exclude"'.(($IPFilter=='exclude')?' checked> ':'> ').'<span style="cursor:pointer" onclick="n=this.previousSibling.previousSibling;n.checked=true;n.onclick()">'.cfCaption('IPFilterExclude')).'</span>';
  365. echo '<div id="excludeInnerDiv" style="display:'.(($IPFilter=='exclude')?'inline':'none')."\">\n";
  366. foreach ($IPFilterExcluded as $key => $value) if($value!=''){
  367.     echo '<span style="margin:1em"></span>'.outButtonSmall('','javascript:removeExcludedIP(\''.$key.'\')',outIcon('cancel'), cfCaption('genDelete')).'<span style="margin:2em;vertical-align:top">'.$value.'</span>'."<br/>";
  368. }
  369. echo cfCaption('IPFilterAdd');
  370. echo '<input style="margin: 0 2em 0 2em" class="textInput" type="text" id="newExclude" name="newExclude" size="12" maxlength="15">';
  371. echo outButton('','javascript:addExclude()',outIcon('add'),false,false,'style="vertical-align:bottom"');
  372. echo '</div></div>';
  373.  
  374. // Local network only
  375. echo outDivFrame('frame2');
  376. echo outFrameHeaderTable('frame2Header','<input onclick="javascript:changeFilter(\'LAN\')" type="radio" name="mode" value="LAN"'.(($isLAN)?' checked> ':'> ').'<span style="cursor:pointer" onclick="n=this.previousSibling.previousSibling;n.checked=true;n.onclick()">'.cfCaption('IPFilterLANOnly')).'</span>';
  377. echo '</div>';
  378.  
  379.  
  380. // Save / Cancel buttons
  381.     echo '<br/><center>';
  382.     echo '<span id="saveButton" style="display:none">'.outButton(cfCaption('genSave'),'javascript:save();',outIcon('save'),false).'</span>';
  383.     echo '<span id="saveButtonD" style="display:inline">'.outButtonDisabled(cfCaption('genSave'),false,outIcon('save'),false).'</span>';
  384.     if(!cfIsInApp()) echo  '<span style="padding-left:5em;"></span>'.outButton(cfCaption('genCancel'),'javascript:winMe.closeMe()',outIcon('cancel'));
  385.     // Close button: return 1 if any filter set, 0 if none (used by uiUsers.php to update IP filter button's icon)
  386.     else echo  '<span style="padding-left:5em;"></span>'.outButton(cfCaption('genClose'),"javascript:wl.UICommand('close:'+((dgi('noFilterInput').checked)?0:1))",outIcon('cancel'));
  387.     echo  '</center>';
  388.  
  389. echo '</form>';
  390. echo '</div>'; // frame1
  391. echo '</div>'; // topDiv
  392. ?>
  393. <script type="text/javascript">
  394. dgi("newExclude").onkeydown=addExclude;
  395. dgi("newInclude").onkeydown=addInclude;
  396. </script>
  397. <?php
  398. echo '</body></html>';
  399. }
  400.  
  401. /**
  402.  * @desc remove background images for selected theme
  403.  *
  404.  * @param string $theme
  405.  */
  406. function wallpaperFormClearImage($theme){
  407.     @unlink(cfAppDocRoot().'/themes/'.$theme.'/wallpaper-centered.jpg');
  408.     @unlink(cfAppDocRoot().'/themes/'.$theme.'/wallpaper-stretched.jpg');
  409.     @unlink(cfAppDocRoot().'/themes/'.$theme.'/wallpaper-tiled.jpg');
  410.     @unlink(cfAppDocRoot().'/themes/'.$theme.'/wallpaper-centered.gif');
  411.     @unlink(cfAppDocRoot().'/themes/'.$theme.'/wallpaper-stretched.gif');
  412.     @unlink(cfAppDocRoot().'/themes/'.$theme.'/wallpaper-tiled.gif');
  413.     @unlink(cfAppDocRoot().'/themes/'.$theme.'/wallpaper-centered.png');
  414.     @unlink(cfAppDocRoot().'/themes/'.$theme.'/wallpaper-stretched.png');
  415.     @unlink(cfAppDocRoot().'/themes/'.$theme.'/wallpaper-tiled.png');
  416. }
  417.  
  418. /**
  419.  * @desc Theme wallpaper selection form
  420.  *         Can be called by /local/wallpaper.php script (from application config panel button)
  421.  */
  422. function wallpaperForm($wpTheme=false){
  423.     // Get theme
  424.     if(!$wpTheme){
  425.         $wpTheme=cfGGetVar('theme');
  426.         if(isset($_GET['theme'])) $wpTheme=$_GET['theme'];
  427.         if(isset($_POST['theme'])) $wpTheme=$_POST['theme'];
  428.         if(!is_dir(cfAppDocRoot().'/themes/'.$wpTheme) ||
  429.         strtolower(cfAppDocRoot().'/themes/'.$wpTheme)!=strtolower(str_replace('\\','/',realpath(cfAppDocRoot().'/themes/'.$wpTheme)))){
  430.             die('Theme not set');
  431.         }
  432.     }
  433.     // Analyze current wallpaper
  434.     $wallpaper=false;
  435.     $wallpaperPosition='centered';
  436.     if(file_exists(cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-centered.jpg')){$wallpaper='/themes/'.$wpTheme.'/wallpaper-centered.jpg'; $wallpaperPosition='centered';}
  437.     elseif(file_exists(cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-stretched.jpg')){$wallpaper='/themes/'.$wpTheme.'/wallpaper-stretched.jpg'; $wallpaperPosition='stretched';}
  438.     elseif(file_exists(cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-tiled.jpg')){$wallpaper='/themes/'.$wpTheme.'/wallpaper-tiled.jpg'; $wallpaperPosition='tiled';}
  439.     if(file_exists(cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-centered.gif')){$wallpaper='/themes/'.$wpTheme.'/wallpaper-centered.gif'; $wallpaperPosition='centered';}
  440.     elseif(file_exists(cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-stretched.gif')){$wallpaper='/themes/'.$wpTheme.'/wallpaper-stretched.gif'; $wallpaperPosition='stretched';}
  441.     elseif(file_exists(cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-tiled.gif')){$wallpaper='/themes/'.$wpTheme.'/wallpaper-tiled.gif'; $wallpaperPosition='tiled';}
  442.     if(file_exists(cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-centered.png')){$wallpaper='/themes/'.$wpTheme.'/wallpaper-centered.png'; $wallpaperPosition='centered';}
  443.     elseif(file_exists(cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-stretched.png')){$wallpaper='/themes/'.$wpTheme.'/wallpaper-stretched.png'; $wallpaperPosition='stretched';}
  444.     elseif(file_exists(cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-tiled.png')){$wallpaper='/themes/'.$wpTheme.'/wallpaper-tiled.png'; $wallpaperPosition='tiled';}
  445.     if(!$wallpaperPosition) $wallpaperPosition='centered';
  446.  
  447.  
  448.     /*
  449.      ***************************************************************************************************************************
  450.      * Process completed upload
  451.      ***************************************************************************************************************************
  452.      */
  453.     if(isset($_POST['useWallpaper']) && isset($_POST['position']) && isset($_FILES['image'])) {
  454.         if($_POST['useWallpaper']=='no'){
  455.             wallpaperFormClearImage($wpTheme);
  456.             if(cfIsInApp())
  457.                 echo '<script language="javascript" type="text/javascript">parent.openWPForm("'.$wpTheme.'")</script>';
  458.             else
  459.                 echo '<script language="javascript" type="text/javascript">parent.location.reload();</script>';
  460.             exit;
  461.         }
  462.         else{
  463.  
  464.             // new image sent
  465.             if(strlen($_FILES['image']['name']) && !empty($_FILES['image']['tmp_name']) && is_uploaded_file($_FILES['image']['tmp_name'])){
  466.  
  467.                 if($_FILES['image']['type']!='image/jpeg' && $_FILES['image']['type']!='image/pjpeg' && $_FILES['image']['type']!='image/gif' && $_FILES['image']['type']!='image/png'){
  468.                     $incorrectFileType=true;
  469.                 }
  470.                 elseif($_POST['position']=='centered' || $_POST['position']=='stretched' || $_POST['position']=='tiled'){
  471.                     if(move_uploaded_file($_FILES['image']['tmp_name'], cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-temp.'.cfFileExtension($_FILES['image']['name']))){
  472.                         // Remove previous wallpaper
  473.                         wallpaperFormClearImage($wpTheme);
  474.  
  475.                         // Copy uploaded wallpaper
  476.                         rename(cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-temp.'.cfFileExtension($_FILES['image']['name']), cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-'.$_POST['position'].'.'.((cfFileExtension($_FILES['image']['name'])=='jpeg')?'jpg':cfFileExtension($_FILES['image']['name'])));
  477.  
  478.                         if(cfIsInApp())
  479.                             echo '<script language="javascript" type="text/javascript">parent.openWPForm("'.$wpTheme.'")</script>';
  480.                         else
  481.                             echo '<script language="javascript" type="text/javascript">parent.location.reload()</script>';
  482.                         exit;
  483.                     }
  484.                 }
  485.             }
  486.             // no new image
  487.             else{
  488.                 // Change current wallpaper position
  489.                 if($wallpaper && $_POST['position']=='centered' || $_POST['position']=='stretched' || $_POST['position']=='tiled'){
  490.                     @rename(cfAppDocRoot().$wallpaper,cfAppDocRoot().'/themes/'.$wpTheme.'/wallpaper-'.$_POST['position'].'.'.cfFileExtension($wallpaper));
  491.                     if(cfIsInApp())
  492.                         echo '<script language="javascript" type="text/javascript">parent.openWPForm("'.$wpTheme.'")</script>';
  493.                     else
  494.                         echo '<script language="javascript" type="text/javascript">parent.location.reload();</script>';
  495.                     exit;
  496.                 }
  497.             }
  498.         }
  499.     }
  500.  
  501.     // <head>
  502.     cfInsertHEAD(false);
  503.     echo '<meta width="350x290"></meta>';
  504.     echo '<meta wintitle="'.cfCaption('genWallpaper').'"></meta>';
  505.     if(!cfIsInApp()) echo cfScriptLink('winClient.js');
  506.     echo "</HEAD>\n";
  507.  
  508.     // <body>
  509.     echo '<body '.((!cfIsInApp())?' onload="winMe.setSize(380,'.((cfGetBrowser()=='ie')?310:280).');"':' oncontextmenu="return false;"').'>';
  510.  
  511.     if(!$wallpaper) $wallpaper=outIcon('appIcon');
  512.  
  513.     // <script>
  514.     ?>
  515.     <script language="Javascript" type="text/javascript">
  516.     var currentWallpaper="<?php echo $wallpaper.'?rnd='.rand();?>";
  517.     function checkFile(){
  518.         if(dgi('image').value.substr(dgi('image').value.length-4).toLowerCase()!='.gif' && dgi('image').value.substr(dgi('image').value.length-4).toLowerCase()!='.jpg' && dgi('image').value.substr(dgi('image').value.length-5).toLowerCase()!='.jpeg' && dgi('image').value.substr(dgi('image').value.length-4).toLowerCase()!='.png'){
  519.             alert("<?php echo cfCaptionJS('blogErrorNotImage');?>");
  520.             dgi('image').value="";
  521.         }
  522.         dgi('doUse').checked=true;
  523.     }
  524.     function setPosition(val){
  525.         if(val=='no'){
  526.             dgi("doUse").checked=false;
  527.             dgi('preview').innerHTML='';
  528.             return;
  529.         }
  530.         if(!val){
  531.             if(dgi('pc').checked) val='centered';
  532.             if(dgi('ps').checked) val='stretched';
  533.             if(dgi('pt').checked) val='tiled';
  534.         }
  535.         dgi("doUse").checked=true;
  536.         if(!currentWallpaper) return;
  537.         if(val=='centered') dgi('preview').innerHTML='<img style="position:relative;width:60px;height:40px;top:15px;left:5px" src="'+currentWallpaper+'">';
  538.         if(val=='stretched') dgi('preview').innerHTML='<img style="position:absolute;width:70px;height:70px;top:0px;left:0px" src="'+currentWallpaper+'">'
  539.         if(val=='tiled') dgi('preview').innerHTML='<img style="position:absolute;width:50px;height:40px;top:0px;left:0px" src="'+currentWallpaper+'"><img style="position:absolute;width:50px;height:40px;top:0px;left:51px" src="'+currentWallpaper+'"><img style="position:absolute;width:50px;height:40px;top:41px;left:0px" src="'+currentWallpaper+'"><img style="position:absolute;width:50px;height:40px;top:41px;left:51px" src="'+currentWallpaper+'">';
  540.     }
  541.  
  542.     </script>
  543.     <?php
  544.     echo outDivFrame('frame1',false,'margin-top:10px');
  545.     echo '<div class="frame1Header">'.outImage(outIcon('wallpaper'),false,false,'margin-right:1em; vertical-align:middle').cfCaption('genWallpaper').'</div>';
  546.  
  547.     if(file_exists(cfAppDocRoot().'/themes/'.$wpTheme.'/menu.php')) require_once('../themes/'.$wpTheme.'/menu.php');
  548.  
  549.     // No Wallpaper allowed
  550.     if(isset($theme['hasWallpaper']) && !$theme['hasWallpaper']){
  551.         echo '<div style="width:100%; text-align:center"><br/><br/><span class="warning">'.cfCaption('configNoWallpaper').'</span><br/><br/><br/><br/>';
  552.         echo outButton(cfCaption('genClose'),'javascript:'.((cfIsInApp())?'parent.openWPForm(\''.$wpTheme.'\')':'winMe.closeMe();'),outIcon('cancel'));
  553.         echo '</div>';
  554.     }
  555.     // Upload form
  556.     else{
  557.         // Image upload form
  558.         echo '<form name="wallpaperForm" method="POST" enctype="multipart/form-data" style="margin:0;padding:0">';
  559.  
  560.         // No image
  561.         echo outDivFrame('frame2');
  562.         echo '<input type="radio" id="noUse" name="useWallpaper" value="no"'.(($wallpaper)?'':' checked="checked"').' onclick="setPosition(\'no\')"><span style="cursor:default" onclick="setPosition(\'no\')">'.cfCaption('configWPNo').'</span><br/>';
  563.         echo '</div>';
  564.  
  565.         // Send image
  566.         echo outDivFrame('frame2');
  567.         echo '<input type="radio" id="doUse" name="useWallpaper" value="yes"'.((!$wallpaper)?'':' checked="checked"').' onclick="setPosition()"><span style="cursor:default" onclick="setPosition()">'.cfCaption('configSelectWallpaper').'</span><br/><br/>';
  568.         echo '<input type="hidden" name="theme" value="'.$wpTheme.'">';
  569.  
  570.         // Image selection control
  571.         echo '<input onchange="checkFile();" type="file" id="image" name="image" accept="image/gif,image/jpeg" value="" size=5>';
  572.         echo '<div class="warning">';
  573.         if(isset($incorrectFileType)) echo cfCaption('blogErrorNotImage'); else echo ' ';
  574.         echo '</div>';
  575.         echo '<div id="preview" style="position:relative;width:70px;height:70px;margin-bottom:20px;float:left;border:1px solid #AAA;overflow:hidden;background:#EEE"></div>';
  576.         // Position selection controls
  577.         echo '<input type="radio" onclick="setPosition(this.value)" id="pc" name="position" value="centered"'.(($wallpaperPosition=='centered')?' checked="checked"':'').'>'.cfCaption('configWPCentered').'  ';
  578.         echo '<input type="radio" onclick="setPosition(this.value)" id="pt" name="position" value="tiled"'.(($wallpaperPosition=='tiled')?' checked="checked"':'').'>'.cfCaption('configWPTiled').'  ';
  579.         echo '<input type="radio" onclick="setPosition(this.value)" id="ps" name="position" value="stretched"'.(($wallpaperPosition=='stretched')?' checked="checked"':'').'>'.cfCaption('configWPStretched');
  580.         echo '</div>';
  581.         echo "</form>\n";
  582.         echo '<center>';
  583.         echo outButton(cfCaption('genValidate'),'javascript:D.wallpaperForm.submit();',outIcon('ok'));
  584.         echo outButton(cfCaption('genCancel'),'javascript:'.((cfIsInApp())?'parent.openWPForm(\''.$wpTheme.'\')':'winMe.closeMe();'),outIcon('cancel'),false,false,'style="margin-left:2em"');
  585.     }
  586.     if($wallpaper) echo '<script language="Javascript" type="text/javascript">setPosition("'.$wallpaperPosition.'");</script>';
  587.     echo '</div></body></html>';
  588. }
  589.  
  590. /**
  591.  * @desc display theme selection Form
  592.  *
  593.  * @param string $environment : 'application' if launched from UI, 'browser' if launched from web browser
  594.  * @param mixed $userFile : - false for general theme
  595.  *                             - user configuration file name for user theme
  596.  *                             - or user ID for for user theme
  597.  * @param boolean $wallpaperSelection: true to allow user to select wallpaper
  598.  */
  599. function acThemeForm($environment, $userFile=false, $wallpaperSelection=true){
  600.     // Init themes list
  601.     $themes=cfMGetVar('weezoThemes');
  602.     unset($themes['wiizo']); unset($themes['skins/default']);
  603.  
  604.     /*
  605.      ***************************************************************************************************************************
  606.      * Process POST commands
  607.      ***************************************************************************************************************************
  608.      */
  609.     if(isset($_POST['selectedTheme']) && isset($_POST['userFile'])){
  610.         if(($_POST['selectedTheme']=='-' || isset($themes[$_POST['selectedTheme']])) && ($_POST['userFile']=='general' || (file_exists(cfAppDataDir().'/'.$_POST['userFile']) && cfFileExtension($_POST['userFile'])=='usr'))){
  611.             if($_POST['selectedTheme']=='-') $_POST['selectedTheme']='';
  612.             // General theme
  613.             if($_POST['userFile']=='general'){
  614.                 cfGUpdateVar('theme',$_POST['selectedTheme']);
  615.             }
  616.             // User theme
  617.             else{
  618.                 require_once(INCLUDE_DIR.'resourceConfigFunctions.php');
  619.                 $uc=new WUserConfig($_POST['userFile']);
  620.                 if($uc->isValid()){
  621.                     // Modify current user config
  622.                     if(cfIsInApp()) $uc->setVar('theme',$_POST['selectedTheme']);
  623.  
  624.                     // Single user account: commit to user configuration
  625.                     if(cfIsInApp() || cfUGetVar('accountType')=='singleUser') $uc->save();
  626.                     if(WEnv::user()) WEnv::user()->setVar('theme',$_POST['selectedTheme']);
  627.                     //cfUSetVar('theme',$_POST['selectedTheme']);
  628.                 }
  629.             }
  630.             // Close page (app environement)
  631.             cfInsertHEAD(false);
  632.             if(cfIsInApp()) die('<script type="text/javascript">wl.UIWebPopupReturn("'.$_POST['selectedTheme'].'")</script>');
  633.             echo cfScriptLink('winClient.js');
  634.             die('<script type="text/javascript">winMe.reloadDesktop()</script>');
  635.         }
  636.     }
  637.  
  638.  
  639.     // Load parameters
  640.     if($userFile===false || $userFile=='general'){
  641.         $userFile='general';
  642.         $selectedTheme=cfGGetVar('theme');
  643.         $themeLevel='general';
  644.     }
  645.     elseif(file_exists(cfAppDataDir().'/'.$userFile) && cfFileExtension($userFile)=='usr'){
  646.         $userData=cfParse_ini_file(cfAppDataDir().'/'.$userFile);
  647.         if(isset($userData['theme'])) $selectedTheme=$userData['theme']; else $selectedTheme=false;
  648.         $themeLevel='user';
  649.     }
  650.     else outDisplayErrorPage(cfCaption('configInvalidFile'));
  651.  
  652.     /*
  653.      ***************************************************************************************************************************
  654.      * HEAD, CSS & JS
  655.      ***************************************************************************************************************************
  656.      */
  657.     cfInsertHEAD(false);
  658.     echo '<meta width="800x550"></meta>';
  659.     echo '<meta icon="theme"></meta>';
  660.     echo '<meta wintitle="'.cfCaption('genTheme').'"></meta>';
  661.     if(!cfIsInApp()) echo cfScriptLink('winClient.js');
  662.     echo "</HEAD>\n";
  663.     ?>
  664.     <style type="text/css">
  665.     .WPIframe{display:none; overflow:hide;margin-left:-15px;margin-right:-15px;margin-top:8px;margin-bottom:-7px;width:220px;padding:-5px}
  666.     .ts {padding:10px;margin:5px; border:1px solid #DDF; background:#F9F9FF;vertical-align:top}
  667.     .tsh{padding:10px;margin:5px; border:1px solid #AAF; background:#DDF;vertical-align:top}
  668.  
  669.     .tss{padding:10px;margin:5px; border:1px solid #AAF; background:#CCF;vertical-align:top}
  670.     .tssh{padding:10px;margin:5px; border:1px solid #88C; background:#AAD;vertical-align:top}
  671.  
  672.     .tsc{padding:10px;margin:5px; border:1px solid #AAF; background:#99D;vertical-align:top}
  673.  
  674.     .thumbnail, .noPreview{border:1px solid #DDD; margin:3px 10px 10px 10px;width:200;height:153}
  675.     #themesTable span{}
  676.     .noPreview{display:block;position:relative;width:200px;height:153px;font-weight:bold;padding-top:70px;text-align:center; background:white;margin:3px 10px 10px 10px;}
  677.     .tsh .noPreview, .tssh .noPreview {border:1px solid #AAA !important}
  678.     {border:1px solid #DDD; margin:10px;}
  679.  
  680.     </style>
  681.     <script type="text/javascript">
  682.     function hi(item){
  683.         if(item.className=='ts') item.className='tsh'; else item.className='tssh';
  684.     }
  685.     function low(item){
  686.         if(item.className=='tsh') item.className='ts'; else item.className='tss';
  687.     }
  688.     function select(item){
  689.         item.className='tsc';
  690.         dgn("selectedTheme").value=item.id;
  691.         D.themeForm.submit();
  692.     }
  693.     function keyPressed(e) {
  694.         if(wl.eventKC(e)==27){
  695.             <?php
  696.             if(cfIsInApp()) echo 'javascript:wl.UICommand(\'close\')'; else echo 'winMe.closeMe();';
  697.             ?>
  698.         }
  699.     }
  700.     function openWPForm(id){
  701.         if(dgi('wallpaper_'+id).style.display=='inline'){
  702.             anim(id, 10, 'close');
  703.             return;
  704.         }
  705.         dgi('wallpaper_'+id).src='/local/wallpaper.php?theme='+id;
  706.         anim(id, 0, 'open');
  707.     }
  708.     function anim(id, step, dir){
  709.         if(step==0 && dir=='open')    dgi('wallpaper_'+id).style.display='inline';
  710.         if(step==10 && dir=='close')    dgi('img_'+id).style.display='inline';
  711.         dgi('wallpaper_'+id).style.height=((step)*26)+'px';
  712.         dgi('img_'+id).style.height=(153*(1-step/10))+'px';
  713.         dgi('img_'+id).style.width='200px';
  714.         if(dir=='open'){
  715.             if(step<10)    window.setTimeout('anim("'+id+'",'+(step+1)+',"open")',10);
  716.             else dgi('img_'+id).style.display='none';
  717.         }
  718.         if(dir=='close') {
  719.             if(step>0) window.setTimeout('anim("'+id+'",'+(step-1)+',"close")',10);
  720.             else dgi('wallpaper_'+id).style.display='none';
  721.         }
  722.     }
  723.     </script>
  724.     <body onKeyPress="keyPressed(event);">
  725.     <form name="themeForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data" onsubmit="return false;" style="display:none">
  726.     <input type="text" name="userFile" value="<?php echo $userFile;?>">
  727.     <input type="text" name="selectedTheme">
  728.     </form>
  729.  
  730.     <?php
  731.     echo outDivFrame('frame1');
  732.     echo '<table id="themesTable" class="frame1" style="background:none; border:none; text-align:center" cellspacing="10">';
  733.     $nb=0;
  734.  
  735.     /**
  736.      * If user-level or resource-level theme, display a "default theme" box
  737.      */
  738.     if($themeLevel!='general'){
  739.         echo '<tr><td id="-" onmouseover="hi(this)" onmouseout="low(this)" onclick="select(this)" class="'.(($selectedTheme==false)?'tss':'ts').'">';
  740.         if(isset($themes[cfGGetVar('theme')]) && file_exists($themes[cfGGetVar('theme')]['dir'].'/thumbnail.png'))
  741.             $path=$themes[cfGGetVar('theme')]['path'].'/thumbnail.png';
  742.         else
  743.             $path=false;
  744.         echo '<div style="font-weight:bold">'.cfCaption('genDefaultValue').'</div>';
  745.         if($path) echo '<img src="'.$path.'" style="'.outCssOpacity(30).'" class="thumbnail"/><br>';
  746.         else echo '<div class="noPreview">'.cfCaption('genNoPreview').'</div>';
  747.  
  748.         echo '</td>';
  749.         $nb=1;
  750.     }
  751.  
  752.     /*
  753.      ***************************************************************************************************************************
  754.      * Display themes thumbnails
  755.      ***************************************************************************************************************************
  756.      */
  757.     foreach ($themes as $key=>$value) if($key!='wiizo' && $key!='skins/default' && (!isset($value['private']) || !$value['private'])) {
  758.         if($nb%3==0) echo '<tr>';
  759.         if(file_exists($value['dir'].'/thumbnail.png')) $path=$value['path'].'/thumbnail.png'; else $path=false;
  760.         echo '<td id="'.$key.'" onmouseover="hi(this)" onmouseout="low(this)" onclick="select(this)" class="'.(($selectedTheme==$key)?'tss':'ts').'">';
  761.         echo '<div style="margin:0;padding:0">'.ucfirst(cfUTF8Encode($key)).'</div>';
  762.         if($path) echo '<img id="img_'.$key.'" src="'.$path.'" class="thumbnail"/><br>'; else echo '<div id="img_'.$key.'" class="noPreview">'.cfCaption('genNoPreview').'</div>';
  763.         if($wallpaperSelection){
  764.             echo outButton(cfCaption('genWallpaper'),'javascript:openWPForm(\''.$key.'\')',outIcon('wallpaper'));
  765.             echo '<iframe id="wallpaper_'.$key.'" class="WPIframe" frameborder="0"></iframe>';
  766.         }
  767.         echo '</td>';
  768.         $nb++;
  769.         if($nb%3==0) echo '</tr>';
  770.     }
  771.     if($nb%3!=0) echo '</tr>';
  772.     echo '</table>';
  773. }